package com.minisoft.pingan;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.minisoft.utils.DateUtil;
import com.minisoft.utils.reportdata.HttpExportBase;
import lombok.Data;
import org.springframework.http.HttpMethod;

import java.time.LocalDate;
import java.util.*;

/**
 * @author JueSu
 * @description: TODO
 * @date 2023/12/10 12:23
 */
public class ExportBdUrlV2 extends HttpExportBase {

    static String bodyStr = "{\n" +
            "    \"vehicleLicenceCode\": \"\",\n" +
            "    \"queryDateBegin\": \"2023-12-01\",\n" +
            "    \"queryDateEnd\": \"2023-12-05\",\n" +
            "    \"vehicleFrameNo\": \"\",\n" +
            "    \"page\": 1,\n" +
            "    \"rows\": 100,\n" +
            "    \"applicantType\": \"\",\n" +
            "    \"coinsuranceMark\": \"\",\n" +
            "    \"productCode\": \"\",\n" +
            "    \"carBusinessType\": \"\",\n" +
            "    \"taskState\": \"B5\",\n" +
            "    \"subTaskState\": \"\",\n" +
            "    \"personnelAttribute\": \"2\",\n" +
            "    \"applicantName\": \"\",\n" +
            "    \"isProduct\": \"isProduct\",\n" +
            "    \"saleAgentCode\": \"2040006831\",\n" +
            "    \"departmentCode\": \"204151007\",\n" +
            "    \"lowDepartment\": true,\n" +
            "    \"applySeletemodel\": \"0\",\n" +
            "    \"userId\": \"EX-PANTANGMEI488\"\n" +
            "}";
    /**
     * 只需要修改Cookie，fhiUFUxo
     */
    static String Cookie = "SESSION=MGY2NDhhN2QtMmYwOS00OGZjLWI4NmQtMTU0MTk0OTkxMjEy; us=stable_616; routeopr=cca8617c0c43c123e0c300de4135f0fa; PA_GREY=EX-PANTANGMEI488; BIGipServerPOOL_PACLOUD_PRDR20230106232593=495029207.5162.0000; v1LIHNTx4qTOO=5GhNVuIvcYlF_3nK_BMccau4uBRFQwO1Oy1mIaTtiL_PvBwg2CeICL_VhfuVHBeEdIlEpqavDF7f0lzsKDh9dxa; BIGipServerPOOL_PACLOUD_PRDR2019112805992=1210229207.33420.0000; PAMO_SESSION=516e78ff25da46e2a7f1dd20d16f6c20_v2; hm_sessionid=516e78ff25da46e2a7f1dd20d16f6c20_v2; USER_REDIS_COOKIE=RVgtUEFOVEFOR01FSTQ4ODE3MDY5NTM4MTczNzUtNjI5NjE1MTQwXzE3MDY5NTQ5MzM3MDA%3D; v1LIHNTx4qTOP=FsSPATdWnJSbbSnGW2g1mUu.LMHNgflNf14pRshTcH3MDH002mPUOhbhtbS8MgAYGlUlCbADwYcPQgljhChhQkeYkvQfhksbkvkCV0aOH5a0Wh1.smY5VptRX1bhSqvxbOHX.oIcXTycbc2sPVBEVZN4FiKbz19T30lpXGBq7Qz85ZM08BB1EW9bXWqJbr54hV8Doq08544vTgcjQoVoESUV3SA_PdX0aJ5u0l068k.yHQ5GNwxTYzK2tLHfIDam8RtB3hCACfslT9zpC18AzDzVqccEATvmznCFfvdam.4VaGoioobarvplYPjOvQ_3BRnV9rhGxVeYlB1qIncG.qQVPrh2QxNEWlpKyiSUc5NOG1tJCUB3Ge8ZielnVhbPYm3hVITCrRZNsjoDsnXdLCfOIUnmGjBbgohXXeH6VAQ";
    static String fhiUFUxo = "NpH2FAlqExZ6z_O7frPwEmBTh5g4apb4brKIq_419D4HJKe3QcEFEhQv..bMXnmHpx1hBAG8AruVSeZlPIzThs_nJeOzYq53";
    /**
     * 如果要过滤指定的业务员
     */
    static String inputByIdFilter = "";
    static String url = "https://icorepnbs.pingan.com.cn/icore_pnbs/do/app/workbench/qtWaitTaskInfopai65o";

    public ExportBdUrlV2(String url, HttpMethod method) {
        super(url,method);
    }

    static Map<String,BaoDan> baoDanMap = new HashMap<>();
    public static void main(String[] args) {

        ExportBdUrlV2 exportBean = new ExportBdUrlV2(url, HttpMethod.POST);
        LocalDate[][] localDates = DateUtil.generateDateRange(2024, 1,5);
        JSONObject body = exportBean.getBody();
        for (LocalDate[] localDate : localDates) {
            body.put("queryDateBegin", localDate[0].toString());
            body.put("queryDateEnd", localDate[1].toString());
            exportBean.fetchData();
        }
        exportBean.writeToFile();
    }
    @Override
    protected boolean processItem(JSONObject obj) {
        String vehicleLicenceCode = obj.getString("vehicleLicenceCode");
        String inputById = obj.getString("inputById");
        if (vehicleLicenceCode == null) {
            return false;
        }
        //过滤指定人
        if (StrUtil.isNotBlank(inputByIdFilter) && !inputByIdFilter.contains(inputById)) {
            return false;
        }
        BaoDan baoDan = baoDanMap.get(vehicleLicenceCode);
        if (baoDan == null) {
            baoDan = new BaoDan();
            baoDan.setInputById(inputById);
            baoDan.setDateInput(obj.getString("dateInput"));
            baoDan.setInsuredName(obj.getString("insuredName"));
            baoDan.setApplicantName(obj.getString("applicantName"));
            baoDan.setVehicleLicenceCode(vehicleLicenceCode);
            baoDanMap.put(vehicleLicenceCode,baoDan);
        }
        String techProduct = obj.getString("techProduct");
        String totalActualPremium = obj.getString("totalActualPremium");
        if (techProduct != null) {
            if ("TP0100001".equals(techProduct)) {
                baoDan.setShangYeXian(totalActualPremium);
            } else {
                baoDan.setJiaoQiangXian(totalActualPremium);
            }
        } else {
            baoDan.setYiWaiXian(totalActualPremium);
        }
        return true;
    }

    @Override
    protected void setPage(int page, JSONObject body, Map<String, Object> params) {
        body.put("page",page);
    }

    @Override
    protected JSONArray datalist(JSONObject jsonObject) {
        return jsonObject.getJSONArray("datalist");
    }

    @Override
    protected int getTotalPage(JSONObject jsonObject) {
        return jsonObject.getInteger("pageSize");
    }

    @Override
    protected List<String> getColumnList() {
        return ListUtil.toList("日期","车牌号","商业险","交强险","意外险","投保人","被保人","录单员id");
    }

    @Override
    protected List<List<Object>> getRows() {
        List<List<Object>> rows = new ArrayList<>();
        Collection<ExportBdUrlV2.BaoDan> baoDans = baoDanMap.values();
        for (ExportBdUrlV2.BaoDan baoDan : baoDans) {
            rows.add(baoDan.toRow());
        }
        return rows;
    }

    @Override
    public Map<String, String> getHeader() {
        Map<String, String> header = super.getHeader();
        header.put("Cookie", Cookie);
        header.put("Referer","https://icorepnbs.pingan.com.cn/icore_pnbs/templates/apply_track_new.html?_v=1.2024.0227.31183617069538677031706953867904");
        return header;
    }

    @Override
    public Map<String, Object> getParams() {
        Map<String, Object> params = super.getParams();
        params.put("fhiUFUxo",fhiUFUxo);
        return params;
    }


    @Override
    protected String getDebuggerParams() {
        return StrUtil.format("queryDateBegin={},queryDateEnd={},page={}",getBody().getString("queryDateBegin"),getBody().getString("queryDateEnd"),getBody().getString("page"));
    }

    @Override
    public JSONObject getBody() {
        JSONObject body = super.getBody();
        if (body.isEmpty()) {
            body.putAll(JSON.parseObject(bodyStr));
        }
        return body;
    }

    @Data
    static class BaoDan {
        String dateInput;//日期
        String inputById;//录单人
        String applicantName;//投保人
        String insuredName;//被保人
        String shangYeXian;//商业险
        String vehicleLicenceCode;//车牌号
        String jiaoQiangXian;//交强险
        String yiWaiXian;//意外险

        public List<Object> toRow() {
            return ListUtil.of(dateInput,vehicleLicenceCode,shangYeXian,jiaoQiangXian,yiWaiXian,applicantName,insuredName,inputById);
        }
    }

}
